package com.wuxiaofan.member.api;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuxiaofan.constant.AppId;
import com.wuxiaofan.member.entity.Member;
import com.wuxiaofan.member.entity.custom.MemberCustom;
import com.wuxiaofan.member.service.MemberService;
import com.wuxiaofan.common.vo.ReturnResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.swagger.annotations.Api;

@RestController
@Api("会员")
@RequestMapping("/member")
@Slf4j
public class MemberApi {


    @Autowired
    private MemberService umsMemberService;


    @PostMapping("login")
    public ReturnResult login(@RequestBody Member member) {
        log.info("login param:{}",member);
        return ReturnResult.success("");
    }


    @GetMapping()
    public ReturnResult queryByUsername() {
        log.info("appid:{}", AppId.APP_ID_IOS);
        return ReturnResult.success("");
    }

    @GetMapping("/findById/{id}")
    public ReturnResult findById(@PathVariable("id") String id) {
        Member umsMember = umsMemberService.findById(id);
        return ReturnResult.success("", umsMember);
    }

    @GetMapping("/findByUsername/{username}")
    public ReturnResult findByUsername(@PathVariable("username") String username) {
        Member umsMember = umsMemberService.findByUsername(username);
        return ReturnResult.success("", umsMember);
    }

    @RequestMapping("findByMap")
    public ReturnResult findByMap(@RequestBody JSONObject jsonObject) {
        Map paraMap = new HashMap();
        paraMap.putAll(jsonObject);
        List<MemberCustom> umsMemberCustoms = umsMemberService.findByMap(paraMap);
        return ReturnResult.success("", umsMemberCustoms);
    }

    @RequestMapping("all")
    public ReturnResult all() {
        Map paraMap = new HashMap();
        List<MemberCustom> umsMemberCustoms = umsMemberService.findByMap(paraMap);
        return ReturnResult.success("", umsMemberCustoms);
    }

    @RequestMapping("findByPage")
    public ReturnResult findByPage(@RequestBody JSONObject jsonObject) {
        Page page = new Page(jsonObject.getIntValue("current"), jsonObject.getIntValue("size"));
        Map paraMap = new HashMap();
        paraMap.putAll(jsonObject);
        IPage<MemberCustom> umsMemberCustoms = umsMemberService.findByMap(paraMap, page);
        return ReturnResult.success("", umsMemberCustoms);
    }

    @RequestMapping("save")
    public ReturnResult save(@RequestBody Member umsMember) {
        umsMember = umsMemberService.save(umsMember);
        return ReturnResult.success("保存成功", umsMember);
    }

    @RequestMapping("update")
    public ReturnResult update(@RequestBody JSONObject jsonObject) {
        Map paraMap = jsonObject;
        umsMemberService.updateMapById(paraMap);
        return ReturnResult.success("更新成功");
    }


    @RequestMapping("delById")
    public ReturnResult delById(@RequestBody JSONObject jsonObject) {
        umsMemberService.delById(jsonObject.getString("id"));
        return ReturnResult.success("删除成功");
    }


}
